{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6084075-1df0-498e-b88a-c2b764fe9c35",
   "metadata": {},
   "source": [
    "# 电机模式 Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57c1047e-b14c-4bd6-a9bb-093d6f8990a4",
   "metadata": {},
   "source": [
    "## 初始化电机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0cbf552a-86b9-40b2-b04f-2471fe21b833",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import Motor\n",
    "from robodyno.interfaces import CanBus\n",
    "\n",
    "can = CanBus()\n",
    "motor = Motor(can, 0x10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e28ba6aa-2f7e-4ea2-a325-b82ad85c8abb",
   "metadata": {},
   "source": [
    "## 使能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7c51730-4911-4dda-adb0-ba390cf4bf7e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "motor.enable()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f45a2417-51d2-43b2-8bb7-eccc733cd77b",
   "metadata": {},
   "source": [
    "## 位置模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "86aaba23-f382-44b3-810a-de95e0cbb7b3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "24dde1caf07640cf9398b4c56722db78",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='mode', options=('直接位置', '滤波位置', '轨迹位置'), value='直接位置'), FloatSlide…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(mode = ['直接位置', '滤波位置', '轨迹位置'], pos=(-3.14, 3.14, 0.01))\n",
    "def demo(mode, pos = 0):\n",
    "    if mode == '直接位置':\n",
    "        motor.position_mode()\n",
    "    elif mode == '滤波位置':\n",
    "        motor.position_filter_mode(4)\n",
    "    elif mode == '轨迹位置':\n",
    "        motor.position_track_mode(2, 0.5, 0.5)\n",
    "    motor.set_pos(pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "349424b6-c7a5-4d7e-9f44-a860004c9e37",
   "metadata": {},
   "source": [
    "## 速度模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6f683484-31c5-4703-8a6e-1bdc83a9ed06",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "834135c505d3450d80661d3ff682042d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='mode', options=('直接速度', '匀加减速速度'), value='直接速度'), FloatSlider(valu…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(mode = ['直接速度', '匀加减速速度'], vel=(-3.14, 3.14, 0.01))\n",
    "def demo(mode, vel = 0):\n",
    "    if mode == '直接速度':\n",
    "        motor.velocity_mode()\n",
    "    elif mode == '匀加减速速度':\n",
    "        motor.velocity_ramp_mode(0.5)\n",
    "    motor.set_vel(vel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f2796d8-7afe-449c-90c8-b6fd5aad259d",
   "metadata": {},
   "source": [
    "## 力矩模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "30af852a-02b5-4ed3-9a2f-8067a0e6051a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "276b3b49374f4f4085a28cd570f5464d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='mode', options=('力矩模式',), value='力矩模式'), FloatSlider(value=0.0, de…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(mode = ['力矩模式'], torque=(-3, 3, 0.01))\n",
    "def demo(mode, torque = 0):\n",
    "    if mode == '力矩模式':\n",
    "        motor.torque_mode()\n",
    "    motor.set_torque(torque/100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2cff862-dc1c-4204-b1e3-7bbeb1180fa0",
   "metadata": {},
   "source": [
    "## 读取电机数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "78c42e1a-0eca-4bb3-b1e8-8f10fc6280a6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "49e638a523b54cb18b8b48c9a4199506",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='data', options=('位置', '绝对位置', '速度', '力矩', '电压', '温度', '模式'), value…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(data = ['位置', '绝对位置', '速度', '力矩', '电压', '温度', '模式'])\n",
    "def demo(data):\n",
    "    if data == '位置':\n",
    "        print(motor.get_pos(1))\n",
    "    elif data == '绝对位置':\n",
    "        print(motor.get_abs_pos(1))\n",
    "    elif data == '速度':\n",
    "        print(motor.get_vel(1))\n",
    "    elif data == '力矩':\n",
    "        print(motor.get_torque(1))\n",
    "    elif data == '电压':\n",
    "        print(motor.get_voltage(1))\n",
    "    elif data == '温度':\n",
    "        print(motor.get_temperature(1))\n",
    "    elif data == '模式':\n",
    "        print(motor.get_mode(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa8b6cf6-b56e-454d-9ba1-e073eff9414b",
   "metadata": {},
   "source": [
    "## 失能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "61c8ba1a-8241-4bc6-8cad-51e3ed1be577",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "motor.disable()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45b65983-ca94-4dd0-9dec-6ff63eca81f0",
   "metadata": {},
   "source": [
    "## 断开CAN总线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "124a8ede-e92b-40c0-9aa6-f0c857539733",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "can.disconnect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
